package com.chalmers.irc2you.model;

import java.util.LinkedList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {

	private boolean inHilights, inHilight, inChannelList, inChannel, inUser,
			inTimeStamp, inMsg, inName, inRow, inRows, inNick;

	private Channel channel;
	private LinkedList<Message> messages;
	private LinkedList<String> channels;
	private String nick, user, timestamp, msg, name;

	public Channel getChannel() {
		return channel;
	}

	public LinkedList<Message> getHilights() {
		return messages;
	}

	public String getNick() {
		return nick;
	}

	public LinkedList<String> getChannels() {
		return channels;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.equals("hilight")) {
			inHilight = true;
		} else if (localName.equals("hilights")) {
			inHilights = true;
		} else if (localName.equals("channel")) {
			inChannel = true;
		} else if (localName.equals("channelList")) {
			inChannelList = true;
		} else if (localName.equals("from")) {
			inUser = true;
		} else if (localName.equals("timestamp")) {
			inTimeStamp = true;
		} else if (localName.equals("msg")) {
			inMsg = true;
		} else if (localName.equals("name")) {
			inName = true;
		} else if (localName.equals("arow")) {
			inRow = true;
		} else if (localName.equals("nick")) {
			inNick = true;
		} else if (localName.equals("rows")) {
			inRows = true;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("hilight")) {
			messages.add(new Message(true, name, timestamp, user, msg));
			inHilight = false;
		} else if (localName.equals("channel")) {
			if (!inHilight && !inChannelList) {
				channel = new Channel(name, messages);
			}
			inChannel = false;
		} else if (localName.equals("from")) {
			inUser = false;
		} else if (localName.equals("timestamp")) {
			inTimeStamp = false;
		} else if (localName.equals("msg")) {
			inMsg = false;
		} else if (localName.equals("name")) {
			inName = false;
		} else if (localName.equals("arow")) {
			if(inRow && inChannel){
				//TODO fix sender...
				messages.add(new Message(false, name, timestamp, "oklart", msg));
			}
			inRow = false;
		} else if (localName.equals("nick")) {
			inNick = false;
		} else if (localName.equals("rows")) {
			inRows = false;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String chars = new String(ch, start, length);
		chars = chars.trim();
		if (inNick) {
			nick = chars;
		} else if (inHilights) {
			if (inHilight) {
				if (inUser) {
					user = chars;
				} else if (inChannel) {
					name = chars;
				} else if (inMsg) {
					msg = chars;
				}
			}
		} else if (inChannelList) {
			if (inChannel) {
				channels.add(chars);
			}
			// TODO fix when bark has fixed
		} else if (inChannel && !inChannelList) {
			if (inName) {
				name = chars;
			} else if (inRows) {
				if (inRow) {
					if (inMsg) {
						msg = chars;
					} else if (inTimeStamp) {
						timestamp = chars;
					}
				}
			}
		}
	}
}